<!--
 * @version: 
 * @Author: yu
 * @Date: 2023-07-12 10:43:56
 * @LastEditors: Andy
 * @LastEditTime: 2023-07-12 14:53:13
-->
<template>
    <el-dialog :model-value="props.isShow" :title="props.isAdd ? '添加角色' : '编辑角色'" :before-close="close">
        <el-form :model="form" :rules="rules" ref="roleRef">
            <el-form-item label="角色名称" :label-width="formLabelWidth" prop="rolename">
                <el-input v-model="form.rolename" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="角色权限" :label-width="formLabelWidth">
                <el-tree default-expand-all :data="store.getters['menu/getMenu']" show-checkbox node-key="id"
                    :props="{ children: 'children', label: 'title' }" ref="tree">
                </el-tree>
            </el-form-item>


            <el-form-item label="状态" :label-width="formLabelWidth">
                <el-switch v-model="form.status" active-color="#13ce66" inactive-color="#ccc" :active-value="1"
                    :inactive-value="2">
                </el-switch>
            </el-form-item>

        </el-form>

        <template #footer>
            <span class="dialog-footer">
                <el-button @click="cancel(roleRef)">取消</el-button>
                <el-button type="primary" @click="confirm(roleRef)">确 定</el-button>

            </span>
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { defineProps, ref, reactive, defineEmits, defineExpose, onMounted } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { useStore } from 'vuex'
import { ElMessage } from "element-plus";
import { roleAdd, roleInfo, roleEdit } from '@/request/api'

//定义父子组件接收
let props = defineProps({
    isShow: Boolean,
    isAdd: Boolean
})
//定义子父组件接收
let emits = defineEmits(['cancel'])
let roleRef = ref<FormInstance>()
let tree = ref<any>()
let store = useStore()
// console.log(store);
store.dispatch('menu/listActions')

let typeId = 0

interface RuleForm {
    rolename: string,
    menus: string,
    status: number
}

const rules = reactive<FormRules<RuleForm>>({
    rolename: [
        { required: true, message: '请输入菜角色名称', trigger: 'blur' },
        { min: 2, max: 8, message: '字符串长度是2到8', trigger: 'blur' },
    ],
})

//定义数据
let form = ref({
    rolename: '',  //角色名称
    menus: '',  //角色权限 id数组
    status: 1,  //状态1正常2禁用
})

const formLabelWidth = '120px'


//取消
const cancel = (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    emits('cancel', false)
    form.value = {
        rolename: '',  //角色名称
        menus: '',  //角色权限 id数组
        status: 1,  //状态1正常2禁用
    }
    //清空规则验证
    formEl.resetFields()
    tree.value.setCheckedKeys([])
    // console.log(tree.value);


}

const close = (done: any) => {
    done;
    cancel(roleRef.value)
}

//确认
const confirm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    await formEl.validate((val) => {
        // console.log(val);
        if (val) {
            form.value.menus = tree.value.getCheckedKeys().join()
            if (props.isAdd) {
                // console.log(form.value);
                roleAdd(form.value).then((res: any) => {
                    // console.log(res);
                    if (res.code === 200) {
                        //消息提示
                        ElMessage.success(res.msg)
                        //取消弹窗
                        cancel(roleRef.value)
                        //更新列表
                        store.dispatch('role/listActions')
                    }
                })
            } else {
                roleEdit({ ...form.value, id: typeId }).then((res: any) => {
                    if (res.code === 200) {
                        ElMessage.success(res.msg)
                        cancel(roleRef.value)
                        store.dispatch('role/listActions')

                    }
                })
            }



        }

    })
}

//回显
const getDetail = (id: any) => {
    // console.log(id);
    typeId = id
    roleInfo({ id }).then((res: any) => {
        // console.log(res);
        if (res.code === 200) {
            form.value = res.list
            tree.value.setCheckedKeys(form.value.menus ? form.value.menus.split(',') : [])
        }

    })

}
defineExpose({
    getDetail
})


</script>

<style></style>